/**********************************************************************************************
Author             Version            Date                Update
Arun Ramachandran     V1.0            April 20,2011        To update the status of campaign member based on Task disposition.
Arun Ramachandran     V1.1            May 13, 2011         To update the status of the Campaign member based on combination of Task disposition and Call Resolution.


***********************************************************************************************/
Public class CXGCampaignMemberOperation{
           
public static void CampaignMemberStatusUpdate(List<Task> tskList){

 //Declare a set to capture all the ID's of the Campaign Members.
     set<Id> Campid = new Set<Id>(); 
     List<CXG_Campaign_Accounts__c> CxG = New List<CXG_Campaign_Accounts__c>();     
     String Campaign_Members_Prefix = GeneralConstants.Campaign_Member_Prefix;
 
 //Declare a list to store the data about the task to be created on the Account.
     List<Task> TaskList = New List<Task>();
 
 //Capture all the ID's of the Campaign Members related to the associated task.
     for(Task tsk :tskList)
         {
              String CMWhatId=tsk.Whatid;               
              if(CMWhatId.startswith(Campaign_Members_Prefix)){
              campid.add(tsk.whatId); 
              }
         }
 
 //Declare a Map and update it with the ID of the Campaign Member
     Map<Id,CXG_Campaign_Accounts__c> CampStatus = New Map<ID,CXG_Campaign_Accounts__c>();
 
 //Declare a map to store the Campaign Id and The Customer__c field ID.
     Map<Id,ID> Accts = New Map<ID,ID>();

     for(CXG_Campaign_Accounts__c ctr:[Select Id,Status__c,Customer__c, Disposition__c,Call_Resolution__c from CXG_Campaign_Accounts__c where ID IN: CampID]) 
         { 
              CampStatus.put(ctr.id,ctr);
               
         }
    
//Verify the value in the Disposition Log of the task and update the corresponding value for Status in Campaign member.  
     for(Task tsk:tskList)
         {    
              String CMWhatId=tsk.Whatid;               
              if(CMWhatId.startswith(Campaign_Members_Prefix)){
//Criteria for fixing the Status, Disposition and Call resolution of the campaign member by verifying against the Disposition and Call resolution field on the Task.
            
              CXG_Campaign_Accounts__c memb = new CXG_Campaign_Accounts__c();     
             if(Tsk.Disposition__c=='Left Message 1'||Tsk.Disposition__c=='Ring No Answer 1'||Tsk.Disposition__c=='Ring No Answer 2' )
                {
                   CampStatus.get(tsk.whatid).status__C = 'In Progress - Not Contacted'; 
                   CampStatus.get(tsk.whatid).Disposition__c= Tsk.Disposition__c;
                   CampStatus.get(tsk.whatid).Call_Resolution__c=Tsk.Call_Resolutions__c; 
                   CampStatus.get(tsk.whatid).Assigned_to__c=Tsk.Ownerid;     
                }
             if((Tsk.Disposition__c=='Customer Contacted'||Tsk.Disposition__c=='Customer Contacted'&& Tsk.Call_Resolutions__c=='Pending Resolution'))
                {
                   CampStatus.get(tsk.whatid).status__C = 'In Progress - Contacted'; 
                   CampStatus.get(tsk.whatid).Disposition__c= Tsk.Disposition__c;
                   CampStatus.get(tsk.whatid).Call_Resolution__c=Tsk.Call_Resolutions__c; 
                   CampStatus.get(tsk.whatid).Assigned_to__c=Tsk.Ownerid;
                }
             if(Tsk.Disposition__c=='Left Message 2'||Tsk.Disposition__c=='Line Disconnected / Fax Line'||Tsk.Disposition__c=='Add to Do Not Call List'||Tsk.Disposition__c=='Ring No Answer 3' ||Tsk.Disposition__c=='D2')
                {
                   CampStatus.get(tsk.whatid).status__C = 'Closed'; 
                   CampStatus.get(tsk.whatid).Disposition__c= Tsk.Disposition__c;
                   CampStatus.get(tsk.whatid).Call_Resolution__c=Tsk.Call_Resolutions__c;   
                   CampStatus.get(tsk.whatid).Assigned_to__c=Tsk.Ownerid;   
                } 
             if((Tsk.Disposition__c=='Customer Contacted'&& Tsk.Call_Resolutions__c=='No Resolution Needed')||(Tsk.Disposition__c=='Customer Contacted'&& Tsk.Call_Resolutions__c=='Unable to Resolve')||(Tsk.Disposition__c=='Customer Contacted'&& Tsk.Call_Resolutions__c=='Specialist Resolution')||Tsk.Disposition__c=='Customer Contacted- No Issue'||(Tsk.Disposition__c=='Customer Contacted'&& Tsk.Call_Resolutions__c=='Warm Transfer- Save Desk')||(Tsk.Disposition__c=='Customer Contacted'&& Tsk.Call_Resolutions__c=='Warm Transfer- Other')||(Tsk.Disposition__c=='Customer Contacted'&& Tsk.Call_Resolutions__c=='Referral to Sales'))
             {
                   CampStatus.get(tsk.whatid).status__C = 'Completed'; 
                   CampStatus.get(tsk.whatid).Disposition__c= Tsk.Disposition__c;
                   CampStatus.get(tsk.whatid).Call_Resolution__c=Tsk.Call_Resolutions__c;
                   CampStatus.get(tsk.whatid).Assigned_to__c=Tsk.Ownerid;
             }        
                //Adding the values in the list.            
     CxG.add(CampStatus.get(tsk.whatid)); 
            
//Creating a new Task on the Account and copying the values in the task fields on the Account.            
    // Task t = new Task();
     //t.owner=tsk.owner;
     //t.what=tsk.what;
          
   //  t.subject=tsk.subject;
   //  t.Description=tsk.Description;
   //  t.ActivityDate=tsk.ActivityDate;
   //  t.status=tsk.status;
   //  t.whatid=accts.get(tsk.whatid);
   //  TaskList.add(t);   
   }          
}
//Update the List
    try
    {
        update CxG;
    
//The command insert is used since the Task is supposed to be created.    
   // insert TaskList;
      
    }

    catch(DMLexception e)
    {
        system.debug(e);
    }

}

public static void CampaignMemberCallsIncrement(List<Task> tskList){
//Declare a set to capture all the ID's of the Campaign Members.
     set<Id> Campid = new Set<Id>(); 
     List<CXG_Campaign_Accounts__c> CxG1 = New List<CXG_Campaign_Accounts__c>();    
//Declare a Map and update it with the ID of the Campaign Member
     Map<Id,CXG_Campaign_Accounts__c> CampStatus = New Map<ID,CXG_Campaign_Accounts__c>();

     
     //Capture all the ID's of the Campaign Members related to the associated task.
     for(Task tsk :tskList)
         {
                campid.add(tsk.whatId); 
                system.debug('Camp Set has : '+campid);
         }    
          
     for(CXG_Campaign_Accounts__c ctr:[Select Id,Number_of_Call_Attempts_Made__c from CXG_Campaign_Accounts__c where Id IN: Campid]) 
         { 
              CampStatus.put(ctr.id,ctr);                   
         }
     
      /*-------------------------------------------------------------------------------------------------*/    
  List<Id> Campmember = New List<Id>();
  List<Id> CampmemberList = New List<Id>();
  
     for(Task tsk:tskList)
         {
         String CMWhatId=tsk.Whatid;
         if(Tsk.Disposition__c=='Left Message 1'||Tsk.Disposition__c=='Left Message 2'||Tsk.Disposition__c=='Ring No Answer 1'||Tsk.Disposition__c=='Ring No Answer 2'||Tsk.Disposition__c=='Line Disconnected / Fax Line'||Tsk.Disposition__c=='Add to Do Not Call List'||Tsk.Disposition__c=='Ring No Answer 3')
             { 
                    Campmember.add(tsk.whatid)    ;
                    system.debug('PPPPP'+Campmember);
                    
             }
         
         if(Tsk.Disposition__c=='Customer Contacted'||Tsk.Disposition__c=='Customer Contacted- No Issue')
             { 
                    CampmemberList.add(tsk.whatid)    ;
                    system.debug('QQQQQ'+CampmemberList);

             }
         }
     if(Campmember.size()>0){   
     for(CXG_Campaign_Accounts__c Calls:[Select Number_of_Call_Attempts_Made__c from CXG_Campaign_Accounts__c where id in:Campmember])
         {    
                    Calls.Number_of_Call_Attempts_Made__c+= 1;
                    CxG1.add(Calls);
                    System.debug('LLLLL'+CxG1);
         } 
     }
     if(CampmemberList.size()>0){    
     for(CXG_Campaign_Accounts__c Calls:[Select Number_of_Call_Attempts_Made__c,Number_of_times_Contacted__c from CXG_Campaign_Accounts__c where id in:CampmemberList])
         {    
                    Calls.Number_of_times_Contacted__c+= 1;
                    Calls.Number_of_Call_Attempts_Made__c+=1;
                    CxG1.add(Calls);
                    System.debug('L2L2L2'+CxG1);
         }    
     } 
     try
     {    
         Update CxG1;   
     }
     Catch(DMLexception e)
     {
         System.debug(e);
     }

}}